<?php
class BlogCats extends CActiveRecord {
    
    public $catId;
    public $name;
    public $url;
    public $title;
    public $keywords;
    public $description;
    public $about;
    public $date;
    
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    
    public function beforeValidate() {
        if(!parent::beforeValidate()) return false;
        $this->attributes = !empty($_POST[get_class($this)]) ? $_POST[get_class($this)] : array();
        return true;
    }
    
    public function rules() {
        return array(
            array('name, url', 'required', 'message' => '{attribute} не может быть пустым'),
            array('title, description, keywords, about', 'safe'),
            array('date', 'type', 'type' => 'date', 'message' => 'Не верный формат даты', 'dateFormat' => 'dd.MM.yyyy'),
        );
    }
    
    public function attributeLabels() {
        return array(
            'keywords'=>'Содержимое keywords',
            'description'=>'Содержимое description',
            'title'=>'Содержимое title',
            'about'=>'Описание',
            'name'=>'Нзвание',
            'url'=>'Урл',
        );
    }

    public function tableName() {
        return 'blog_cats';
    }
    
    public function beforeSave() {
        if(!parent::beforeSave()) return false;
        if(!empty($this->date)) {
            $date = explode('.', $this->date);
            $this->date = mktime(null, null, null, $date[1], $date[0], $date[2]);
        } else $this->date = time();
        return true;
    }
    
    public function afterFind() {
        $this->date = date('d.m.Y', $this->date);
    }
}
